unit u_resennemi;

interface

type

  r_ennemi = record
    x, y, z, id : integer;
    typ : string;
  end;

  t_ennemi = Array of r_ennemi;

  c_res_ennemi = class
    private
      ennemi_tab : t_ennemi;
    public
      map_client : array of array of string;
      procedure get_ennemi_info(var inf : t_ennemi);
      procedure get_ennemi_by_typ(_typ : string; var inf : t_ennemi);
      procedure change_ennemi_pos(last_x, last_y, last_z, new_x, new_y, new_z : integer);
      procedure add_ennemi(n_x, n_y, n_z  : integer; typ : string);
      procedure del_ennemi_pos(pos_x, pos_y, pos_z : integer);
      function id_to_coord(id_ : integer) : r_ennemi;
      function coord_to_id(x_, y_, z_ : integer) : integer;
      constructor create;
      procedure set_taille(x_, y_ : integer);
  end;

implementation

uses
  u_engine;

  constructor c_res_ennemi.create;
    begin
      inherited create;
      SetLength(ennemi_tab, 0);
    end;

  procedure c_res_ennemi.change_ennemi_pos(last_x, last_y, last_z, new_x, new_y, new_z : integer);
  var
    i : integer;
    begin
      i := 0;

      if Length(ennemi_tab) <> 0 then
        while (i < Length(ennemi_tab)) and
        not((ennemi_tab[i].x = last_x) and (ennemi_tab[i].y = last_y) and (ennemi_tab[i].z = last_z)) do
          i := i + 1;
        if i <> Length(ennemi_tab) then
          begin
            if myplayer.etage = last_z then
              begin
                myplayer.myreseauennemis.map_client[last_x, last_y] := 'none';
                myplayer.myreseauennemis.map_client[new_x, new_y] := ennemi_tab[i].typ;
              end;
            ennemi_tab[i].x := new_x;
            ennemi_tab[i].y := new_y;
            ennemi_tab[i].z := new_z;
          end;
    end;

  procedure c_res_ennemi.add_ennemi(n_x, n_y, n_z : Integer; typ : string);
  var
    x : integer;
    begin
      x := Length(ennemi_tab);
      SetLength(ennemi_tab, x + 1);
      ennemi_tab[x].x := n_x;
      ennemi_tab[x].y := n_y;
      ennemi_tab[x].z := n_z;
      ennemi_tab[x].id := x;
      ennemi_tab[x].typ := typ;
      if myplayer.etage = n_z then
        map_client[n_x, n_y] := typ;
    end;

  procedure c_res_ennemi.del_ennemi_pos(pos_x, pos_y, pos_z : Integer);
  var
    i, j, len : integer;
    begin
      i := 0;
      while (i < Length(ennemi_tab)) and
      not((ennemi_tab[i].x = pos_x) and (ennemi_tab[i].y = pos_y) and (ennemi_tab[i].z = pos_z)) do
        i := i + 1;
      if Length(ennemi_tab) <> i then
        begin
          for j := i to Length(ennemi_tab) - 2 do
            ennemi_tab[j] := ennemi_tab[j + 1];
          SetLength(ennemi_tab, length(ennemi_tab) - 1);
        end;
    end;

  procedure c_res_ennemi.get_ennemi_info(var inf : t_ennemi);
    begin
      inf := ennemi_tab;
    end;

  procedure c_res_ennemi.get_ennemi_by_typ(_typ: string; var inf: t_ennemi);
  var
  i, len : Integer;
    begin
      SetLength(inf, 0);
      len := 0;
      for i := 0 to Length(ennemi_tab) - 1 do
        if ennemi_tab[i].typ = _typ then
          begin
            len := len + 1;
            SetLength(inf, len);
            inf[len - 1] := ennemi_tab[i];
          end;
    end;

  function c_res_ennemi.id_to_coord(id_ : integer) :  r_ennemi;
  var
    i : integer;
    begin
      i := 0;
      if Length(ennemi_tab) <> 0 then
        begin
          while (i <= High(ennemi_tab)) and (id_ <> ennemi_tab[i].id) do
            i := i + 1;
          if i < Length(ennemi_tab) then    
            Result := ennemi_tab[i];
        end;          
    end;

  function c_res_ennemi.coord_to_id(x_: Integer; y_: Integer; z_: Integer) : integer;
  var
    i : integer;
    begin
      i := 0;
      if Length(ennemi_tab) <> 0 then
        begin
          while (i <= High(ennemi_tab)) and not((ennemi_tab[i].x = x_) and (ennemi_tab[i].y = y_) and (ennemi_tab[i].z = z_)) do
            i := i + 1;
          if i < Length(ennemi_tab) then
            Result := ennemi_tab[i].id;          
        end;
    end;

  procedure c_res_ennemi.set_taille(x_: Integer; y_: Integer);
  var
    i, j : integer;
    begin
      SetLength(map_client, x_, y_);
      for i := low(map_client) to high(map_client) do
        for j := low(map_client[i]) to high(map_client[i]) do
          map_client[i,j] := 'none';
    end;

end.

